import {
    ADD_BAR,
    ADD_CLICK_ROUTE,
    BAR_LIST,
    CURRENT_BAR,
    DEL_All_BAR,
    DEL_BAR,
    SET_BAR_LIST,
    SET_CURRENT_BAR,
} from "@/store/modules/tabbar/type";
import { buildFullPath } from "@/utils/queryParams";

const index = {
    namespaced: true,
    state: {
        moreMenuList: [],
        hasClickMenuList: [],
        barList: JSON.parse(sessionStorage.getItem(BAR_LIST)) || [
            {
                barCode: "/home",
                barName: "首页",
                name: "Home",
                path: "/home",
                fullPath: "/home",
                meta: { title: "首页", affix: true },
            },
        ],
        currentBar: JSON.parse(sessionStorage.getItem(CURRENT_BAR)) || {
            barCode: "/home",
            barName: "首页",
            name: "Home",
            path: "/home",
            fullPath: "/home",
            meta: { title: "首页", affix: true },
        },
    },
    mutations: {
        [SET_BAR_LIST](state, data) {
            state.barList = data;
        },
        [SET_CURRENT_BAR](state, currentBar) {
            state.currentBar = currentBar;
            sessionStorage.setItem(
                CURRENT_BAR,
                JSON.stringify(state.currentBar),
            );
        },
        [ADD_BAR](state, content) {
            // 构造唯一的标签标识符，包括查询参数
            const barCode =
                content.fullPath || buildFullPath(content.path, content.query);

            // 不允许重复，每个标签只允许存在一个
            let ind = state.barList.findIndex((el) => el.barCode === barCode);
            if (ind === -1) {
                // 添加新的标签
                state.barList.push({
                    ...content,
                    barCode: barCode,
                });
            } else {
                // 如果标签已存在，更新当前标签
                state.currentBar = state.barList[ind];
                sessionStorage.setItem(
                    CURRENT_BAR,
                    JSON.stringify(state.currentBar),
                );
                return;
            }

            sessionStorage.setItem(BAR_LIST, JSON.stringify(state.barList));

            // 设置当前标签
            state.currentBar = {
                ...content,
                barCode: barCode,
            };
            sessionStorage.setItem(
                CURRENT_BAR,
                JSON.stringify(state.currentBar),
            );
        },
        [DEL_BAR](state, barCode) {
            console.log("删除之前" + state.barList.length);
            // 不删除固定标签
            // const deletingTag = state.barList.find(
            //     (item) => item.barCode === barCode,
            // );
            state.barList = state.barList.filter(
                (item) =>
                    item.barCode !== barCode || (item.meta && item.meta.affix),
            );
            console.log("删除之后" + state.barList.length);
            sessionStorage.setItem(BAR_LIST, JSON.stringify(state.barList));

            // 如果删除的是当前标签，需要更新当前标签
            if (state.currentBar.barCode === barCode) {
                // 设置为首页或其他标签
                const homeTag = state.barList.find(
                    (item) => item.path === "/home",
                );
                if (homeTag) {
                    state.currentBar = homeTag;
                } else if (state.barList.length > 0) {
                    // 如果删除的不是最后一个标签，选择下一个或上一个标签
                    const deletingIndex = state.barList.findIndex(
                        (item) => item.barCode === barCode,
                    );
                    if (deletingIndex < state.barList.length) {
                        state.currentBar = state.barList[deletingIndex];
                    } else if (state.barList.length > 0) {
                        state.currentBar =
                            state.barList[state.barList.length - 1];
                    }
                } else {
                    // 如果没有其他标签了，重置为默认首页
                    state.currentBar = {
                        barCode: "/home",
                        barName: "首页",
                        name: "Home",
                        path: "/home",
                        fullPath: "/home",
                        meta: { title: "首页", affix: true },
                    };
                }
                sessionStorage.setItem(
                    CURRENT_BAR,
                    JSON.stringify(state.currentBar),
                );

                // 触发路由跳转到新的当前标签
                if (
                    typeof window !== "undefined" &&
                    window.vm &&
                    window.vm.$router
                ) {
                    window.vm.$router.push(
                        state.currentBar.fullPath || state.currentBar.path,
                    );
                }
            }
        },
        [DEL_All_BAR](state, content) {
            state.barList = (
                content || [
                    {
                        barCode: "/home",
                        barName: "首页",
                        name: "Home",
                        path: "/home",
                        fullPath: "/home",
                        meta: { title: "首页", affix: true },
                    },
                ]
            ).filter((item) => item.meta && item.meta.affix); // 保留固定标签
            sessionStorage.setItem(BAR_LIST, JSON.stringify(state.barList));
        },

        [ADD_CLICK_ROUTE](state, clickRoute) {
            // 不允许重复，每个标签只允许存在一个
            let ind = state.hasClickMenuList.findIndex(
                (el) => el.clickCode === clickRoute.clickCode,
            );
            if (ind === -1) {
                // 添加新的标签
                state.hasClickMenuList.push({
                    ...clickRoute,
                    clickCode: clickRoute.clickCode,
                });
            }
        },
    },
    actions: {
        [ADD_BAR]({ commit }, content) {
            console.log("add tar:" + content);
            commit(ADD_BAR, content);
        },
        [DEL_BAR]({ commit }, barCode) {
            commit(DEL_BAR, barCode);
        },
        [SET_CURRENT_BAR]({ commit }, currentBar) {
            commit(SET_CURRENT_BAR, currentBar);
        },
        [ADD_CLICK_ROUTE]({ commit }, clickRoute) {
            commit(ADD_CLICK_ROUTE, clickRoute);
        },
    },
};

export default index;
